package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ToggleButton;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.AddressListAdapter;
import com.iexbuy.ihg.entity.AddressObject;
import com.iexbuy.ihg.entity.AddressObjectResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeAddresssDetailFragment extends BaseFragment {

	private static WeDeAddresssDetailFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.name)
	private EditText name;
	@ViewInject(R.id.mobileNo)
	private EditText mobileNo;
	@ViewInject(R.id.toggleButton)
	private ToggleButton toggleButton;
	@ViewInject(R.id.areaNo)
	private EditText areaNo;
	@ViewInject(R.id.address)
	private EditText address;


	private static AddressObject addressObject;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_address_detail, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_address_detail));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);
		mBaseActivity.setRightButton("保存", new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				submitAddress();
			}
		});

		if (!"-1".equalsIgnoreCase(addressObject.getAddressId())) {
			name.setText(addressObject.getReceiver());
			mobileNo.setText(addressObject.getMobile());
			toggleButton.setChecked("1".equalsIgnoreCase(addressObject.getDefAddr()) ? true : false);
			areaNo.setText(addressObject.getZip());
			address.setText(addressObject.getAddress());
		}
	}


	public void submitAddress() {
		if (StringUtil.isEmpty(name.getText().toString())) {
			ToastUtils.toast(mBaseActivity, "请输入收件人姓名");
			return;
		}
		if (StringUtil.isEmpty(mobileNo.getText().toString())) {
			ToastUtils.toast(mBaseActivity, "请输入联系电话");
			return;
		}
		if (StringUtil.isEmpty(address.getText().toString())) {
			ToastUtils.toast(mBaseActivity, "请输入地址");
			return;
		}

		HashMap<String, String> paramMap = new HashMap<String, String>();
		if (!"-1".equalsIgnoreCase(addressObject.getAddressId())) {
			paramMap.put("addressId", addressObject.getAddressId());
		}
		paramMap.put("receiver", name.getText().toString());
		paramMap.put("mobile", mobileNo.getText().toString());
		paramMap.put("defAddr", toggleButton.isChecked()?"1":"0");
		paramMap.put("address", address.getText().toString());
		paramMap.put("zip", areaNo.getText().toString());
		ServiceHelper.send(mBaseActivity, ServiceHelper.SUBMIT_ADDRESS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						ToastUtils.toast(mBaseActivity, "保存成功");
						mBaseActivity.finish();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	public static void setAddressObject(AddressObject addressObject) {
		WeDeAddresssDetailFragment.addressObject = addressObject;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeAddresssDetailFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
